const vscode = require("vscode");
const util = require("../util.js");
const item = require("../item/index.js");


class User {
  constructor() {
    this.name = "点击登录";
    this.id = "";
    this.auth = false;
    this.content = null;
    this.statusBar = null;
    this.item = new item.daodanItem();
    this.userMap={}
  }
  setctx(ct) {
    this.content = ct;
    this.item.setctx(ct)
    // 获取缓存数据，得到登录态
    let userInfo = ct.globalState.get("user_userinfo");
    if (userInfo) {
      // 有登录态，刷新登录数据和项目信息
      this._getLocalData(userInfo);
    }
    const um=ct.globalState.get("user_userMap");
    if(um){
      this.userMap=JSON.parse(um)
    }
    // 初始化登录相关方法
    ct.subscriptions.push(this.init());
    ct.subscriptions.push(this.login());
    ct.subscriptions.push(this.logout());
  }
  setName(params) {
    this.name = params;
  }
  logout() {
    return vscode.commands.registerCommand("daodan.logout", () => {
      vscode.window
        .showInformationMessage(
          "你好，" + this.name + "\n是否登出",
          "登出",
          "取消"
        )
        .then((c) => {
          if (c == "登出") {
            this.auth = false;
            this.name = "点击登录";
            this.statusBar.text = "$(error)" + this.name;
            this.statusBar.command = "daodan.login";
            this.content.globalState.update("user_userinfo", null);
            this.item.refresh(0);
          }
        });
    });
  }
  init() {
    this.statusBar = vscode.window.createStatusBarItem(
      vscode.StatusBarAlignment.Left,
      100
    );
    if (this.auth) {
      this.statusBar.text = this.name;
      this.statusBar.command = "daodan.logout";
    } else {
      this.statusBar.text = "$(error)" + this.name;
      this.statusBar.command = "daodan.login";
    }

    this.statusBar.show();
    return this.statusBar;
  }
  login() {
    return vscode.commands.registerCommand("daodan.login", () => {
      vscode.window
        .showInputBox({
          prompt: "请输入您的称呼",
          placeHolder: "新用户直接输入当做注册",
        })
        .then((name) => {
          if (name) {
            name = name.trim();
            if (name.length == 0) {
              vscode.window.showErrorMessage("称呼长度 1～6，请重新输入～");
              return;
            }
          } else {
            vscode.window.showErrorMessage("称呼长度 1～6，请重新输入～");
            return;
          }

          vscode.window
            .showInputBox({
              prompt: "请输入账号的密码",
              placeHolder: "新用户直接输入当做注册，请记住密码",
            })
            .then((accessval) => {
              if (accessval && accessval.length > 0) {
                let localName = this.content.globalState.get(name);
                if (localName) {
                  localName = JSON.parse(localName);
                  if (localName.accessval != accessval) {
                    vscode.window.showErrorMessage("认证码错误，请重新登录～");
                    return;
                  }
                  this.id = localName.id;
                } else {
                  this.id = util.getID("user");
                  this.content.globalState.update(
                    name,
                    JSON.stringify({ name: name, id: this.id, accessval })
                  );

                }
                this.auth = true;
                this.name = name;
                this.statusBar.text = this.name;
                this.statusBar.command = "daodan.logout";
                this.content.globalState.update(
                  "user_userinfo",
                  JSON.stringify({ name: this.name, id: this.id, accessval })
                );
                this.userMap[this.id]={ name: this.name, id: this.id, accessval }
                this.content.globalState.update(
                  "user_userMap",
                  JSON.stringify(this.userMap)
                );
                this.item.refresh(this.id);
                return;
              } else {
                vscode.window.showErrorMessage("请输入密码～");
              }
            });
        });
    });
  }
  _getLocalData(da) {
    try {
      const val = JSON.parse(da);
      this.name = val.name;
      this.id = val.id;
      this.auth = true;
      this.item.refresh(this.id);
    } catch (err) {
      console.log(err);
    }
  }
}
module.exports = {
  User,
};
